package com.lagou.test04.test04;

import java.io.*;
import java.net.Socket;

public class ServerThread extends Thread {

    private Socket socket;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {

        ObjectInputStream ois  = null;
        ObjectOutputStream oos = null;

        UserMessage message = null;
        User user = null;

        try {
            while(true){
                ois = new ObjectInputStream(socket.getInputStream());
                message = (UserMessage) ois.readObject();
                System.out.println(socket.getInetAddress()+"客户端发送消息为--->"+message);
                user = message.getUser();
                String name = user.getName();
                String password = user.getPassword();
                if ("bye".equals(message.getType())) {
                    break;
                }
                if ("admin".equals(name) && "123456".equals(password)) {
                    message.setType("success");
                }else{
                    message.setType("fail");
                }
                oos = new ObjectOutputStream(socket.getOutputStream());
                oos.writeObject(message);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }





    }
}
